const express = require('express')
const router = express.Router()
const sqlFn = require('../mysql');
// jwt
const jwt = require('jsonwebtoken');
// 导入密钥
const config = require('../secert')

// 首页数据
const homeData = require('../mock/home')

// 获取当前时间（创建时间）
function getCurrentTime() {
    var d = new Date();
    var year = d.getFullYear();
    var month = formatNum(d.getMonth() + 1);
    var day = formatNum(d.getDate());
    var hour = formatNum(d.getHours());
    var minute = formatNum(d.getMinutes());
    var second = formatNum(d.getSeconds());
    return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
function formatNum(num) {
    if (num < 10) {
        return "0" + num;
    } else {
        return num;
    }
}

// 测试接口
router.get('/', (req, res) => {
    res.send('服务器启动成功')
})

/** ------------------------------------- 首页接口 ---------------------------------------- */
router.get('/home/goodsList', (req, res) => {
    res.send({
        code: 200,
        data: homeData
    })
})

/** ------------------------------------- 分类接口 ---------------------------------------- */
/**
 * @method get
 * @param  type：类型  0：蛋糕  1：面包  2：小食  3：配件
 * @param  page：页数
 */
router.get('/class/goodsList', (req, res) => {
    const type = req.query.type || 0
    const page = req.query.page || 1
    const sql = `select * from goods_class where type=? order by id asc limit 10 offset ${(page - 1) * 10}`
    sqlFn(sql, type, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result,
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/** ------------------------------------- 搜索关键词接口 ---------------------------------------- */
/**
 * @method get
 * @param  keyword
 */
router.get('/search/goodsList', (req, res) => {
    const name = req.query.keyword
    const sql = "select * from goods_class where concat(`name`) like '%" + name + "%'"
    sqlFn(sql, name, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result,
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/** ------------------------------------- 商品详情接口 ---------------------------------------- */
/**
 * @method get
 * @param name 商品名称
 */
router.get('/details/goodsInfo', (req, res) => {
    const name = req.query.name
    const sql = 'select * from goods_details where `name`=?'
    sqlFn(sql, name, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0],
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/** ------------------------------------- 用户登录接口 ---------------------------------------- */
/**
 * @method post
 * @data phone 手机号
 * @data password 密码
 */
router.post("/login", (req, res) => {
    const { phone, password } = req.body;
    const sql = "select * from user where `phone`=? and `password`=?";
    const arr = [phone, password];
    sqlFn(sql, arr, result => {
        // 返回用户信息
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0].token
            });
        } else {
            res.send({
                status: 404,
                msg: '信息错误'
            })
        }
    })
})

/** ------------------------------------- 手机号注册接口 ---------------------------------------- */
/**
 * 验证手机号是否已经注册
 * @method post
 * @data phone 用户手机号
 */
router.post("/registered/test", (req, res) => {
    const { phone } = req.body;
    const sql = "select * from user where `phone`=?";
    // 查询手机号是否存在
    sqlFn(sql, phone, result => {
        if (result.length > 0) {
            // 已经注册
            res.send({
                status: 200,
                msg: '手机号已经注册'
            })
        } else {
            // 未注册
            res.send({
                status: 300,
                msg: '手机号未注册'
            })
        }
    })
})

/**
 * 发送验证码
 */
router.post("/code", (req, res) => {
    let code = Math.floor(Math.random() * (9999 - 1000)) + 1000
    res.send({
        status: 200,
        code
    })
})

/**
 * 注册（到数据库中）
 * @method post
 * @data phone 用户手机号
 * @data password 用户密码
 * @data username 用户昵称
 */
router.post("/registered", (req, res) => {
    const { phone, username, password } = req.body
    let imgUrl = '../../static/img/userImg.gif'
    let token = jwt.sign({
        phone,
        username
    }, config.jwtSecret, {
        // 过期时间
        expiresIn: 60 * 1
    })
    let arr = [phone, username, password, imgUrl, token]
    const sql = "insert into user values (null,?,?,?,?,?)"
    sqlFn(sql, arr, result => {
        if (result.affectedRows > 0) {
            // userPhone 用户注册手机号
            let userPhone = phone
            // fraction_num 积分数（注册+100）
            let fraction_num = '100'
            // fraction_desc 积分描述
            let fraction_desc = `注册送积分，积分+${fraction_num}`
            // fraction_createTime 创建时间
            let fraction_createTime = getCurrentTime()
            const fractionArr = [userPhone, fraction_num, fraction_desc, fraction_createTime]
            const fractionSql = 'insert into user_fraction values (null,?,?,?,?)'
            sqlFn(fractionSql, fractionArr, resultFraction => {
                if (resultFraction.affectedRows > 0) {
                    res.send({
                        status: 200,
                        msg: '注册成功'
                    })
                } else {
                    res.send({
                        status: 404,
                        msg: '注册失败'
                    })
                }
            })
        } else {
            res.send({
                status: 400,
                msg: "注册失败"
            })
        }
    })
})

/** ------------------------------------- 获取用户信息接口 ---------------------------------------- */
/**
 * @method get
 * @param phone 用户手机号
 */
router.get('/getInfo', (req, res) => {
    const { phone } = req.query
    const sql = 'select * from user where phone=?'
    sqlFn(sql, phone, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0]
            })
        } else {
            res.send({
                status: 400,
                data: new Error()
            })
        }
    })
})

/** ------------------------------------- 购物车接口 ---------------------------------------- */
/**
 * 添加购物车
 * @method post
 * @param phone 用户手机号
 * @param name 商品名称
 * @param nameEN 商品英文名称
 * @param num 商品数量默认为1
 * @param price 商品价格
 * @param choose 用户选择的规格
 * @param imgUrl 商品图片路径
 * @param desc 商品描述（大小，以及餐具数量）
 */
router.post('/shopcar/add', (req, res) => {
    let { phone, name, nameEN, num, price, choose, imgUrl, desc } = req.body
    const sqlSelect = 'select * from shopcar where phone=? and name=? and choose=?'
    const arrSelect = [phone, name, choose]
    // 查询数据库是否有商品（包括规格也要不同）
    sqlFn(sqlSelect, arrSelect, resultSelect => {
        if (resultSelect.length > 0) {
            // 数据库有，让商品数量 +1
            let goodsNum = Number(resultSelect[0].num) + 1
            const arr = [goodsNum, phone, name, choose]
            const sql = 'update shopcar set num=? where phone=? and name=? and choose=?'
            sqlFn(sql, arr, result => {
                if (result.affectedRows > 0) {
                    res.send({
                        status: 200,
                        msg: "已成功添加购物车"
                    })
                } else {
                    res.send({
                        status: 400,
                        msg: "添加购物车失败"
                    })
                }
            })
        } else {
            // 数据库没有，添加数据库
            const sql = 'insert into shopcar values (null,?,?,?,?,?,?,?,?)'
            const arr = [phone, name, nameEN, num, price, choose, imgUrl, desc]
            sqlFn(sql, arr, result => {
                if (result.affectedRows > 0) {
                    res.send({
                        status: 200,
                        msg: "已成功添加购物车"
                    })
                } else {
                    res.send({
                        status: 400,
                        msg: "添加购物车失败"
                    })
                }
            })
        }
    })
})

/**
 * 购物车数据展示
 * @method get
 * @param phone 手机号
 */
router.get('/shopcar/add', (req, res) => {
    let { phone } = req.query
    const sql = 'select * from shopcar where phone=? order by id desc'
    sqlFn(sql, phone, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 400,
                data: new Error()
            })
        }
    })
})

/**
 * 获取购物车编辑的数据信息
 * @method get
 * @param name 商品名称
 */
router.get('/shopcar/edit', (req, res) => {
    const name = req.query.name
    const sql = 'select * from goods_details where name=?'
    sqlFn(sql, name, result => {
        let oldData = result[0]
        let chooseList = JSON.parse(oldData.chooseList)
        let chooseTips = JSON.parse(oldData.chooseTips)
        let price = JSON.parse(oldData.price)
        let newData = {
            chooseList,
            chooseTips,
            price
        }
        if (result.length > 0) {
            res.send({
                status: 200,
                data: newData
            })
        } else {
            res.send({
                status: 400,
                data: new Error()
            })
        }
    })
})

/**
 * 提交修改后的购物车数据接口
 * @method post
 * @data phone 手机号
 * @data name 商品名称
 * 修改的参数：
 * @data price 价格
 * @data choose 选择规格 (chooseTips)
 * @data num 数量
 */
router.post('/shopcar/edit/commit', (req, res) => {
    // 前端提交的数据
    const { phone, name, price, choose, num } = req.body
    const sqlSelect = 'select * from shopcar where phone=? and name=? and choose=?'
    const arrSelect = [phone, name, choose]
    sqlFn(sqlSelect, arrSelect, result => {
        // 修改后如果选择的规格一样，需要对数据库数据进行合并
        if (result.length > 0) {
            // 有一样的数据（数据库的num = 前端的num）
            const sql = 'update shopcar set num=? where phone=? and name=?'
            const arr = [num, phone, name]
            sqlFn(sql, arr, commitRes => {
                if (commitRes.affectedRows > 0) {
                    res.send({
                        status: 200,
                        msg: "修改成功"
                    })
                } else {
                    res.send({
                        status: 400,
                        msg: "修改失败"
                    })
                }
            })
        } else {
            // 没有一样的数据，把之前的那条数据进行修改
            const sql = 'update shopcar set num=?, price=?, choose=? where phone=? and name=?'
            const arr = [num, price, choose, phone, name]
            sqlFn(sql, arr, commitRes => {
                if (commitRes.affectedRows > 0) {
                    res.send({
                        status: 200,
                        msg: "修改成功"
                    })
                } else {
                    res.send({
                        status: 400,
                        msg: "修改失败"
                    })
                }
            })
        }
    })
})

/**
 * 删除购物车数据接口
 * @method get
 * @param id 购物车数据库id
 */
router.get('/shopcar/del', (req, res) => {
    let id = req.query.id
    let sql = "delete from `shopcar` where `id`=?"
    sqlFn(sql, id, result => {
        if (result.affectedRows === 1) {
            res.send({
                status: 200,
                msg: '删除成功'
            })
        } else {
            res.send({
                status: 500,
                msg: '删除失败'
            })
        }
    })
})

/** ------------------------------------- 收货地址接口 ---------------------------------------- */
/**
 * 展示收货地址数据接口
 * @method get
 * @param userPhone 用户id
 */
router.get('/address', (req, res) => {
    const userPhone = req.query.userPhone
    const sql = 'select * from `address` where `userPhone`=? order by id desc'
    sqlFn(sql, userPhone, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 400,
                data: new Error()
            })
        }
    })
})

/**
 * 订单页获取默认收货地址接口
 * @method get
 * @data userPhone 用户身份
 * @return isDefault = 1
 */
router.get('/order/address/default', (req, res) => {
    const { userPhone } = req.query
    const sql = 'select * from address where isDefault=1 and userPhone=?'
    sqlFn(sql, userPhone, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0]
            })
        } else {
            res.send({
                status: 400,
                data: new Error()
            })
        }
    })
})

/**
 * 添加收货地址接口
 * @method post
 * @data userPhone 用户id
 * @data nick 收货昵称
 * @data phone 收货手机号
 * @data province 省
 * @data city 市
 * @data district 区
 * @data add 收货地址
 * @data isDefault 是否默认
 */
router.post('/address/add', (req, res) => {
    let { userPhone, nick, phone, province, city, district, add, isDefault } = req.body
    isDefault = Number(isDefault)
    // 需要先判断数据库是否有数据
    const sqlSelect = 'select * from address where userPhone=?'
    sqlFn(sqlSelect, userPhone, result => {
        if (result.length > 0) {
            if (isDefault) {
                // 地址为默认，将数据库的所有数据变为0
                const sqlUpdate = 'update `address` set isDefault=0 where userPhone=?'
                sqlFn(sqlUpdate, userPhone, commitRes => {
                    if (commitRes.affectedRows > 0) {
                        const sql = 'insert into address values (null,?,?,?,?,?,?,?,?)'
                        const arr = [userPhone, nick, phone, province, city, district, add, isDefault]
                        sqlFn(sql, arr, result => {
                            if (result.affectedRows > 0) {
                                res.send({
                                    status: 200,
                                    msg: "添加成功"
                                })
                            } else {
                                res.send({
                                    status: 400,
                                    msg: "添加失败"
                                })
                            }
                        })
                    }
                })
            } else {
                const sql = 'insert into address values (null,?,?,?,?,?,?,?,?)'
                const arr = [userPhone, nick, phone, province, city, district, add, isDefault]
                sqlFn(sql, arr, result => {
                    if (result.affectedRows > 0) {
                        res.send({
                            status: 200,
                            msg: "添加成功"
                        })
                    } else {
                        res.send({
                            status: 400,
                            msg: "添加失败"
                        })
                    }
                })
            }
        } else {
            const sql = 'insert into address values (null,?,?,?,?,?,?,?,?)'
            const arr = [userPhone, nick, phone, province, city, district, add, isDefault]
            sqlFn(sql, arr, result => {
                if (result.affectedRows > 0) {
                    res.send({
                        status: 200,
                        msg: "添加成功"
                    })
                } else {
                    res.send({
                        status: 400,
                        msg: "添加失败"
                    })
                }
            })
        }
    })
})

/**
 * 编辑收货地址接口（参数同添加地址的一样)
 */
router.post('/address/edit', (req, res) => {
    // id为地址唯一标识
    let { id, userPhone, nick, phone, province, city, district, add, isDefault } = req.body
    isDefault = Number(isDefault)
    id = Number(id)
    if (isDefault) {
        // 地址为默认，将数据库的所有数据变为0
        const sqlUpdate = 'update address set isDefault=0 where userPhone=?'
        sqlFn(sqlUpdate, userPhone, commitRes => {
            if (commitRes.affectedRows > 0) {
                const sql = 'update `address` set `nick`=?, `phone`=?, `province`=?, `city`=?, `district`=?, `add`=?, `isDefault`=? where `id`=?'
                const arr = [nick, phone, province, city, district, add, isDefault, id]
                sqlFn(sql, arr, commitRes => {
                    if (commitRes.affectedRows > 0) {
                        res.send({
                            status: 200,
                            msg: "修改成功"
                        })
                    } else {
                        res.send({
                            status: 400,
                            msg: "修改失败"
                        })
                    }
                })
            }
        })
    } else {
        const sql = 'update `address` set `nick`=?, `phone`=?, `province`=?, `city`=?, `district`=?, `add`=?, `isDefault`=? where `id`=?'
        const arr = [nick, phone, province, city, district, add, isDefault, id]
        sqlFn(sql, arr, commitRes => {
            if (commitRes.affectedRows > 0) {
                res.send({
                    status: 200,
                    msg: "修改成功"
                })
            } else {
                res.send({
                    status: 400,
                    msg: "修改失败"
                })
            }
        })
    }
})

/**
 * 删除收货地址
 * @method get
 * @param id 地址id（唯一标识）
 */
router.get('/address/del', (req, res) => {
    const id = req.query.id
    const sql = 'delete from `address` where `id`=?'
    sqlFn(sql, id, result => {
        if (result.affectedRows === 1) {
            res.send({
                status: 200,
                msg: '删除成功'
            })
        } else {
            res.send({
                status: 500,
                msg: '删除失败'
            })
        }
    })
})

/**
 * 给地址设为默认
 * @method post
 * @data id 地址id（唯一标识）
 * @data userPhone 用户身份
 */
router.post('/address/default', (req, res) => {
    let { id, userPhone } = req.body
    // 先将满足条件的所有地址都设置为0
    const sqlUpdate = 'update address set isDefault=0 where userPhone=?'
    sqlFn(sqlUpdate, userPhone, commitRes => {
        if (commitRes.affectedRows > 0) {
            const sql = 'update address set isDefault=1 where id=? and userPhone=?'
            const arr = [id, userPhone]
            sqlFn(sql, arr, commitRes2 => {
                if (commitRes2.affectedRows > 0) {
                    res.send({
                        status: 200,
                        msg: "修改成功"
                    })
                } else {
                    res.send({
                        status: 400,
                        msg: "修改失败"
                    })
                }
            })
        }
    })
})

/** --------------------------------------- 订单接口 ---------------------------------------- */
/**
 * 提交订单接口
 * @method post
 * @data userPhone 用户注册手机号（唯一身份）
 * @data goodsInfo 商品信息
 * @data payType 支付方式
 * @data total 支付总金额
 * @data nick 收货昵称
 * @data phone 收货手机号
 * @data address 收货地址
 */
router.post('/order/add', (req, res) => {
    const { userPhone, goodsInfo, payType, total, nick, phone, address } = req.body
    // userPhone, goodsInfo, payType, total, nick, phone, address, code, status
    const sql = 'insert into `order` values (null,?,?,?,?,?,?,?,?,?,?)'
    // 生成订单号
    function setTimeDateFmt(s) {  // 个位数补齐十位数
        return s < 10 ? '0' + s : s
    }
    function createordernum() {
        const now = new Date()
        let month = now.getMonth() + 1
        let day = now.getDate()
        let hour = now.getHours()
        let minutes = now.getMinutes()
        let seconds = now.getSeconds()
        month = setTimeDateFmt(month)
        day = setTimeDateFmt(day)
        hour = setTimeDateFmt(hour)
        minutes = setTimeDateFmt(minutes)
        seconds = setTimeDateFmt(seconds)
        let orderCode = now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 1000000)).toString();
        return orderCode
        //基于年月日时分秒+随机数生成订单编号
    }
    // 订单号
    let code = createordernum()
    // 订单创建时间
    let createTime = getCurrentTime()
    // 状态码( 0=>未支付, 1=>支付成功, 2=>订单已取消 )
    let status = 0
    const arr = [userPhone, goodsInfo, payType, total, nick, phone, address, code, createTime, status]
    if (JSON.parse(req.body.goodsInfo).length < 1) {
        res.send({
            status: 404,
            msg: "添加失败"
        })
        return
    }
    sqlFn(sql, arr, result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "添加成功"
            })
        } else {
            res.send({
                status: 400,
                msg: "添加失败"
            })
        }
    })
})

/**
 * 提交订单后，需要将购物车数据进行删除
 * @method post
 * @param [id]
 */
router.post('/paysuc/shopcar/del', (req, res) => {
    const ids = JSON.parse(req.body.ids)
    const sql = `delete from shopcar where id in (${[...ids].join(',')})`
    sqlFn(sql, null, result => {
        if (result.affectedRows === 1) {
            res.send({
                status: 200,
                msg: '删除成功'
            })
        } else {
            res.send({
                status: 500,
                msg: '删除失败'
            })
        }
    })
})

/**
 * 获取当前订单
 * @method get
 * @param userPhone 用户身份
 */
router.get('/getNowOrder', (req, res) => {
    const { userPhone } = req.query
    const sql = 'select * from `order` where `status`=0 and `userPhone`=? order by `id` desc'
    sqlFn(sql, userPhone, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0]
            })
        } else {
            res.send({
                status: 400,
                data: new Error()
            })
        }
    })
})

/**
 * 获取订单列表数据
 * @method get
 * @param userPhone
 */
router.get('/order/all', (req, res) => {
    const userPhone = req.query.userPhone
    const sql = 'select * from `order` where `userPhone` = ? order by id desc'
    sqlFn(sql, userPhone, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/**
 * 获取未支付的订单 状态码( 0=>未支付, 1=>支付成功, 2=>订单已取消 )
 * @method get
 * @param userPhone
 */
router.get('/order/nopay', (req, res) => {
    const { userPhone } = req.query
    const sql = 'select * from `order` where userPhone = ? and status = 0 order by id desc'
    sqlFn(sql, userPhone, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/**
 * 获取我的订单详情页中的数据
 * @method get
 * @param id 订单id
 */
router.get('/order/details', (req, res) => {
    const { id } = req.query
    const sql = 'select * from `order` where `id` = ?'
    sqlFn(sql, id, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0]
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/**
 * 删除订单接口
 * @method get
 * @param id 该订单id
 */
router.get('/order/del', (req, res) => {
    const { id } = req.query
    let sql = "delete from `order` where `id`=?"
    sqlFn(sql, id, result => {
        if (result.affectedRows === 1) {
            res.send({
                status: 200,
                msg: '删除成功'
            })
        } else {
            res.send({
                status: 500,
                msg: '删除失败'
            })
        }
    })
})

/** --------------------------------------- 积分接口 ---------------------------------------- */
/**
 * 获取积分信息
 * @method get
 * @param userPhone 用户手机号
 */
router.get('/integral', (req, res) => {
    const { userPhone } = req.query
    const sql = 'select * from user_fraction where userPhone=?'
    sqlFn(sql, userPhone, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/** ------------------------------------ 对接支付宝沙箱接口 ----------------------------------------------- */
// 引入支付宝配置信息
const alipaySdk = require('../zfb/zfb_config')
const AlipayFromData = require('alipay-sdk/lib/form').default

router.post('/pay', (req, res) => {
    // 前端给后端的数据（订单号，金额，商品描述等信息）
    let orderId = req.body.orderId
    let orderMoney = req.body.orderMoney
    let orderTitle = req.body.orderTitle
    // 对接支付宝
    const formData = new AlipayFromData()
    formData.setMethod('get')

    // http://192.168.31.245:8080/#/pages/PaySuccess/PaySuccess
    formData.addField('returnUrl', 'http://localhost:8080/#/pages/PaySuccess/PaySuccess?code='+orderId) // 付款成功打开的链接
    formData.addField('bizContent', {
        // 传递的订单号
        outTradeNo: orderId,
        // productCode
        product_code: 'FAST_INSTANT_TRADE_PAY',
        // 金额
        total_amount: orderMoney,
        // 商品标题
        subject: orderTitle,
    });

    const result = alipaySdk.exec(
        'alipay.trade.page.pay',
        {},
        { formData: formData },
    )

    result.then((resp) => {
        res.send({
            success: '支付成功',
            code: 200,
            result: resp
        })
    })
})

/**
 * 支付成功后将订单状态更改
 * @method post
 * @data code 订单号
 */
router.post('/update/orderState', (req, res) => {
    const { code } = req.body
    const sql = 'update `order` set `status`=1 where `code` = ?'
    sqlFn(sql, code, result => {
        if (result.affectedRows > 0) {
            // 根据订单号，查询用户手机号，并用手机号将积分数据添加
            const sqlSelectOrder = 'select userPhone, total from `order` where `code` = ?'
            sqlFn(sqlSelectOrder, code, resultSelect => {
                if (resultSelect.length > 0) {
                    let userPhone = resultSelect[0].userPhone
                    let total = resultSelect[0].total
                    let fraction_desc = `消费${total}元，积分+${total}`
                    let createTime = getCurrentTime()
                    const sqlUpdate = 'insert into `user_fraction` values (null,?,?,?,?)'
                    const arrUpdate = [userPhone, total, fraction_desc, createTime]
                    sqlFn(sqlUpdate, arrUpdate, resultUpdate => {
                        if (resultUpdate.affectedRows > 0) {
                            res.send({
                                status: 200,
                                msg: '支付成功'
                            })
                        } else {
                            res.send({
                                status: 404,
                                msg: ''
                            })
                        }
                    })
                } else {
                    res.send({
                        status: 500,
                        msg: new Error()
                    })
                }
            })
        } else {
            res.send({
                status: 400,
                msg: "支付失败"
            })
        }
    })
})

module.exports = router